package com.erhuogo.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.erhuogo.bean.GridPageBean;
import com.erhuogo.bean.MemCommoBean;
import com.erhuogo.bean.PageBean;
import com.erhuogo.dao.CommodityInfoDao;
import com.erhuogo.entity.CommodityInfo;
import com.erhuogo.entity.Favorite;
import com.erhuogo.enumerate.OpenStatus;
import com.erhuogo.service.CommodityInfoService;
import com.erhuogo.util.DateUtils;

/**
 * 商品信息Service的实现类
 * @author aibinxiao
 * @date 2016年10月26日 上午11:58:00
 */
public class CommodityInfoServiceImpl implements CommodityInfoService {
	// 日志
	private static Logger logger = LoggerFactory.getLogger(CommodityInfoServiceImpl.class);
				
	// 注入CommodityInfoDao
	private CommodityInfoDao commodityInfoDao;
	private CommodityInfo comm=null;

	public CommodityInfoDao getCommodityInfoDao() {
		return commodityInfoDao;
	}

	public void setCommodityInfoDao(CommodityInfoDao commodityInfoDao) {
		this.commodityInfoDao = commodityInfoDao;
	}

	// 1.发布二货商品信息Service实现类
	@Override
	public void saveCommodityInfo(CommodityInfo commodityInfo) throws Exception {
		commodityInfo.setCommendNum(0);
		commodityInfo.setEditTime(DateUtils.DateToStr(new Date()));
		commodityInfo.setIssueTime(DateUtils.DateToStr(new Date()));
		commodityInfo.setMessagesNum(0);
		commodityInfo.setReportNum(0);
		commodityInfo.setReportStatus(OpenStatus.OPEN.getStatusCode());
		commodityInfo.setShieldStatus(OpenStatus.OPEN.getStatusCode());
		commodityInfo.setTradeStatus(OpenStatus.OPEN.getStatusCode());
		commodityInfoDao.saveCommodityInfo(commodityInfo);
	}

	// 2.根据商品名模糊查询商品列表Service实现类
	@Override
	public PageBean searchCommodityByName(String name, String num) {
		int pageNum = 1;//页码默认值是1
		if(num!=null){
			pageNum = Integer.parseInt(num);
		}
		int totalRecords = commodityInfoDao.findCountByName(name);
		PageBean<CommodityInfo> pageBean = new PageBean(pageNum, totalRecords, 12);
		//查询分页后的具体记录
		if(totalRecords <= pageBean.getPageSize()){
			List<CommodityInfo> records = commodityInfoDao.findPageByName(name, pageBean.getStartIndex(), totalRecords);
			pageBean.setRecords(records);
		} else{
			List<CommodityInfo> records = commodityInfoDao.findPageByName(name, pageBean.getStartIndex(), pageBean.getPageSize());
			pageBean.setRecords(records);
		}
		return pageBean;
	}

	// 3.分页条件查询二货信息Service实现类
	@Override
	public GridPageBean<CommodityInfo> findByPage(String pageNo, String pageSize, String id, String oneCategoryId,
			String twoCategoryId, String threeCategoryId, String provinceId, String cityId, String schoolId,
			String tradeWay, String reportStatus, String shieldStatus, String tradeStatus, String startDate,
			String endDate) {
		Integer pNo = 1;
		Integer pSize = 10;
		if(!StringUtils.isEmpty(pageNo) && !StringUtils.isEmpty(pageSize)){
			pNo = Integer.valueOf(pageNo);
			pSize = Integer.valueOf(pageSize);
		}
		int records = commodityInfoDao.countByParams(id, oneCategoryId, twoCategoryId, threeCategoryId, provinceId, cityId,
				schoolId, tradeWay, reportStatus, shieldStatus, tradeStatus, startDate, endDate);
		GridPageBean<CommodityInfo> result = new GridPageBean<CommodityInfo>(pNo, records, pSize);
		//查询分页后的具体记录
		if(records <= result.getPageSize()){
			List<CommodityInfo> rows = commodityInfoDao.findPageByParams(id, oneCategoryId, twoCategoryId, threeCategoryId, provinceId, cityId,
					schoolId, tradeWay, reportStatus, shieldStatus, tradeStatus, startDate, endDate);
			result.setRows(rows);
		} else{
			List<CommodityInfo> rows = commodityInfoDao.findPageByParams(id, oneCategoryId, twoCategoryId, threeCategoryId, provinceId, cityId,
					schoolId, tradeWay, reportStatus, shieldStatus, tradeStatus, startDate, endDate);
			result.setRows(rows);
		}
		result.setPage(pNo);
		result.setRecords(records);
		result.setTotal(result.getTotal());
		return result;
	}

	// 4.更新二货信息的启用状态Service实现类
	@Override
	public void updateStatus(String idStr, String statusStr) throws Exception {
		Integer status = null;
		Integer id = null;
		if(StringUtils.isEmpty(idStr)){
			throw new Exception("id参数不能为空");
		}else{
			id = Integer.valueOf(idStr);
		}
		if(StringUtils.isEmpty(statusStr)){
			throw new Exception("转换状态参数不能为空");
		}
		if(!StringUtils.isEmpty(statusStr)){
			status = Integer.valueOf(statusStr);
		}
		//开启
		if(OpenStatus.OPEN.getStatusCode()==status){
			commodityInfoDao.updateStatus(id, OpenStatus.OPEN.getStatusCode());
		}
		//屏蔽
		if(OpenStatus.SHIELD.getStatusCode()==status){
			commodityInfoDao.updateStatus(id, OpenStatus.SHIELD.getStatusCode());
		}
	}

	@Override
	public CommodityInfo getCommById(String id) {
		comm=commodityInfoDao.getCommById(id);
		return comm;
	}

	@Override
	public void clickCommend(String id) {
		commodityInfoDao.clickCommend(id);	
	}

	@Override
	public boolean judgeCollect(String memberId, String commId) {
		boolean flag=commodityInfoDao.judgeCollect(memberId, commId);
		if(flag){
			return true;
		}
		return false;
	}

	@Override
	public boolean collectComm(Favorite favorite) {
		boolean flag=commodityInfoDao.collectComm(favorite);
		return flag;
		
	}

	@Override
	public boolean cancelComm(String commId, String memberId) {
		boolean flag=commodityInfoDao.cancelComm(commId, memberId);
		return flag;
	}

	
}
